package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.BannerCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.Pos;
import com.vanvalt.payhui.modules.pay.service.PosService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.json.JsonConversion;

/**
 * Pos机
 */
public class PosProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(PosProcess.class);
	/*private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();*/
	
	private UserService userService;
	private PosService posService;
	
	/**
	 * 构造方法.
	 */	
	public PosProcess() {
		super();
	}

	/**
	 * @param posService
	 */
	public PosProcess(PosService posService){
		super();
		this.posService = posService;
	}
	
	/**
	 * @param userService
	 * @param posService
	 */
	public PosProcess(UserService userService, PosService posService){
		super();
		this.userService = userService;
		this.posService = posService;
	}

	/**
	 * POS机列表
	 * @param pageCommand
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand posList(PageCommand pageCommand, RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		// 分页参数
		pageCommand = initPagecommand(pageCommand, requestCommand);
		RowBounds rowBounds = new RowBounds(pageCommand.getPage().getOffset(), 
				pageCommand.getPage().getPageSize());
		
		BannerCommand bannerCommand = new BannerCommand(requestCommand.getObject().toString());
		Map<String, Object> map = bannerCommand.getMap();
		String uid = (String) map.get("uid");
        String currentPage = (String) map.get("currentPage");
        String pageSize = (String) map.get("pageSize");
        
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(currentPage) && StringUtils.isBlank(pageSize)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		/*Integer currentPages = Integer.parseInt(currentPage);
                Integer lineSize = Integer.parseInt(pageSize);*/
                
                //resultList = this.getPosListByType(uid, Constant.POS_TYPE_BIG, currentPages, lineSize);
                resultList = this.getPosListByType(uid, Constant.POS_TYPE_BIG, rowBounds);
         		
         		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeListJSON(resultList), responseCommand);
        	}
        	
        }
	}

	/**
	 * 手刷列表
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand handPosList(PageCommand pageCommand, RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		// 分页参数
		pageCommand = initPagecommand(pageCommand, requestCommand);
		RowBounds rowBounds = new RowBounds(pageCommand.getPage().getOffset(), 
				pageCommand.getPage().getPageSize());
				
		BannerCommand bannerCommand = new BannerCommand(requestCommand.getObject().toString());
		Map<String, Object> map = bannerCommand.getMap();
		String uid = (String) map.get("uid");
        String currentPage = (String) map.get("currentPage");
        String pageSize = (String) map.get("pageSize");
		
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(currentPage) && StringUtils.isBlank(pageSize)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		Integer currentPages = Integer.parseInt(currentPage);
                Integer lineSize = Integer.parseInt(pageSize);
                
                //resultList = this.getPosListByType(uid, Constant.POS_TYPE_SMALL, currentPages, lineSize);
                resultList = this.getPosListByType(uid, Constant.POS_TYPE_SMALL, rowBounds);
         		
         		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeListJSON(resultList), responseCommand);
        	}
        	
        }
	}
	
	/**
	 * 根据POS机类型获取列表信息
	 * @param uid
	 * @param type
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	private List<Map<String,Object>> getPosListByType(String uid, String type, int currentPage, int pageSize){
		
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		Pos posParams = new Pos();
        posParams.setType(type);
        posParams.setDelFlag(Constant.DEL_FLAG_NORMAL); // 正常
        
        Page<Pos> page = posService.findPage(new Page<Pos>(currentPage, pageSize), posParams); 
        
        List<Pos> posList = page.getList();
        
        String baseUrl = Global.getConfig("web.file.download.url");
        
        String projectFrontUrl = Global.getConfig("web.project.front.url");
        
        for(Pos pos:posList){
        	
        	Map<String,Object> resultMap = Maps.newHashMap();
        	
        	resultMap.put("id", pos.getId());
        	resultMap.put("name", pos.getName());
        	// POS机产品图片
        	String pictures = pos.getPictures();
        	String pictureUrl = "";
        	List<String> pictureList = Lists.newArrayList();
        	if(pictures != null && !"".equals(pictures)){
        		pictureList = this.getPictureMap(pictures, baseUrl);
        	} 
        	
        	if(pictureList != null && pictureList.size() > 0){
        		pictureUrl = pictureList.get(0);
        	}
        	
        	resultMap.put("picture", pictureUrl);
        	
        	String link = projectFrontUrl + "/pay/pos/detail?id="+pos.getId()+"&uid="+uid;
        	resultMap.put("link", link);
        	
        	resultList.add(resultMap);
        }
        
		return resultList;
	}
	
	/**
	 * 根据POS机类型获取列表信息
	 * @param uid
	 * @param type
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	private List<Map<String,Object>> getPosListByType(String uid, String type, RowBounds rowBounds){
		
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		Pos posParams = new Pos();
        posParams.setType(type);
        posParams.setDelFlag(Constant.DEL_FLAG_NORMAL); // 正常
        posParams.setOffset(rowBounds.getOffset());
        posParams.setLimit(rowBounds.getLimit());
       /* Page<Pos> page = posService.findPage(new Page<Pos>(currentPage, pageSize), posParams); 
        
        List<Pos> posList = page.getList();*/
        
        List<Pos> posList = posService.findListByPage(posParams);
        
        String baseUrl = Global.getConfig("web.file.download.url");
        
        String projectFrontUrl = Global.getConfig("web.project.front.url");
        
        for(Pos pos:posList){
        	
        	Map<String,Object> resultMap = Maps.newHashMap();
        	
        	resultMap.put("id", pos.getId());
        	resultMap.put("name", pos.getName());
        	// POS机产品图片
        	String pictures = pos.getPictures();
        	String pictureUrl = "";
        	List<String> pictureList = Lists.newArrayList();
        	if(pictures != null && !"".equals(pictures)){
        		pictureList = this.getPictureMap(pictures, baseUrl);
        	} 
        	
        	if(pictureList != null && pictureList.size() > 0){
        		pictureUrl = pictureList.get(0);
        	}
        	
        	resultMap.put("picture", pictureUrl);
        	
        	String link = projectFrontUrl + "/pay/pos/detail?id="+pos.getId()+"&uid="+uid;
        	resultMap.put("link", link);
        	
        	resultList.add(resultMap);
        }
        
		return resultList;
	}
	
	private List<String> getPictureMap(String pictures){
		
		List<String> resultList = Lists.newArrayList();
		
		if(pictures != null && !"".equals(pictures)){
			if(pictures.contains(Constant.SPLIT_FILE_NAME)){
				String[] pictureArray = pictures.split(Constant.SPLIT_FILE_NAME);
				
				for(String pic:pictureArray){
					resultList.add(pic);
				}
			}
		}
		
		return resultList;
	}
	
	private List<String> getPictureMap(String pictures, String baseUrl){
		
		List<String> resultList = Lists.newArrayList();
		
		if(pictures != null && !"".equals(pictures)){
			if(pictures.contains(Constant.SPLIT_FILE_NAME)){
				String[] pictureArray = pictures.split("\\|");
				
				for(String pic:pictureArray){
					if(pic.trim() != null && !"".equals(pic.trim())){
						pic = baseUrl + pic;
						resultList.add(pic);
					}
				}
			}
		}
		
		return resultList;
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public PosService getPosService() {
		return posService;
	}

	public void setPosService(PosService posService) {
		this.posService = posService;
	}

	
}
